package com.example.mydemo103.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mydemo103.Adapter.MyPagerAdapter;
import com.example.mydemo103.DB.UserService;
import com.example.mydemo103.LgoinActivity;
import com.example.mydemo103.MainActivity;
import com.example.mydemo103.R;
import com.example.mydemo103.bean.Book;
import com.example.mydemo103.ui.DetailsFragment.DetailsFragmentOne;
import com.example.mydemo103.ui.DetailsFragment.DetailsFragmentThree;
import com.example.mydemo103.ui.DetailsFragment.DetailsFragmentTwo;
import com.example.mydemo103.ui.borrowbook.borrowFragment;
import com.example.mydemo103.util.Config;

import java.util.ArrayList;
import java.util.List;

public class BookdetailsActivity extends AppCompatActivity {
    private MyPagerAdapter myPagerAdapter;
    private ViewPager viewPager;
    List<Fragment> fragmentList;
    private ImageView love,reduce,add;
    private TextView booknum,booktext;
    int booknum1 = 1;
    private LinearLayout bookdetailsbylinertlayout;
    private ImageView bookdetailsbyleft;
    int[] rbsid={R.id.rdBtn1,R.id.rdBtn2,R.id.rdBtn3};
    private Button btn_borrow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookdetails);


        love=findViewById(R.id.love);
        viewPager=findViewById(R.id.viewpageOfdetails);
        reduce=findViewById(R.id.reduce);
        add=findViewById(R.id.add);
        booknum=findViewById(R.id.booknum);
        btn_borrow=findViewById(R.id.btn_borrow);
        final UserService userService=new UserService(BookdetailsActivity.this);
        btn_borrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Book book=userService.borrowbook(1);
                Config.bookbrow=book;
                Toast.makeText(BookdetailsActivity.this,"添加成功："+book.getBookimg(),Toast.LENGTH_LONG).show();
//                Bundle bundle = new Bundle();
//                bundle.putString("bookimg", "1");
////                bundle.putString("", userNames);
//                borrowFragment fragment1 = new borrowFragment();
//                fragment1.setArguments(bundle);
//                FragmentManager fragmentManager = getSupportFragmentManager();
//                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//                fragmentTransaction.replace(R.id.nav_host_fragment,fragment1);
//
//                Log.e("as:","12");

            }
        });











//        绑定当前View界面的incule Id
        bookdetailsbylinertlayout=findViewById(R.id.bookdetailsbyincule);
//        绑定incule中的控件Id
        bookdetailsbyleft=bookdetailsbylinertlayout.findViewById(R.id.bookdetailsleft);
//        设置点击事件
        bookdetailsbyleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BookdetailsActivity.this, MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
            }
        });





//      点击减少书籍数量
        reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                booknum1 -= 1;
                if(booknum1<1){
                    booknum1=1;
                }
                else {
                    booknum.setText(String.valueOf(booknum1));
                }
            }
        });
//        点击添加书籍数量
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                booknum1 += 1;
                if(booknum1>10){
                    booknum1=10;
                }
                else {
                    booknum.setText(String.valueOf(booknum1));
                }
            }
        });


        //        点击爱心事件
        love.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                love.setImageResource(R.drawable.lovebyture);
            }
        });

//       书籍介绍图片
        fragmentList=new ArrayList<>();
        fragmentList.add(new DetailsFragmentOne());
        fragmentList.add(new DetailsFragmentTwo());
        fragmentList.add(new DetailsFragmentThree());
        myPagerAdapter=new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(myPagerAdapter);
        final RadioButton[] rbs=new RadioButton[3];
        for(int i=0;i<rbs.length;i++){
            rbs[i]=findViewById(rbsid[i]);
            final int finalI = i;
            rbs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem(finalI);
                }
            });
        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                rbs[position].setChecked(true);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//    去除标题栏
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
//        finish();
    }
}